procedure CheckForStack;
begin
  if nPics=0 then begin
    PutMessage('This macro requires a stack.');
    exit;
  end;
  if nSlices=0 then begin
    PutMessage('This window is not a stack.');
    exit
  end;
end;

procedure CheckForSelection;
var 
  x1,y1,x2,y2,LineWidth:integer;
  RoiLeft,RoiTop,RoiWidth,RoiHeight:integer;
begin
  GetRoi(RoiLeft,RoiTop,RoiWidth,RoiHeight);
  GetLine(x1,y1,x2,y2,LineWidth);
  if (RoiWidth=0) or (x1>=0) then begin
    PutMessage('Please select a grain');
    exit;
  end;
end;


macro 'import raw image        [0]';
var
w,h:   integer;
begin
w:= GetNumber('width: ',1200);
h:= GetNumber('height: ',1000);
SetImport('Custom');
SetCustom(w,h,0);
Import('image');
end;



macro 'LUT for 8 gray levels    [8]';
var
  i,j,k1,k2,m:integer;
begin
 for m:=1 to 8 do begin
  k1:=(m-1)*32;
  k2:=m*32-1;
		j:=255-k2+31;
  for i:=k1 to k2 do begin
    RedLUT[i]:=j;
    GreenLUT[i]:=j;
    BlueLUT[i]:=j;
  end;
 end;
UpdateLUT;
end;

macro 'LUT for 10 gray levels  [T]';
var
  i,j,k1,k2,m:integer;
begin
 for m:=1 to 10 do begin
  k1:=(m-1)*25;
  k2:=m*25-1;
		j:=255-k2+24;
  for i:=k1 to k2 do begin
    RedLUT[i]:=j;
    GreenLUT[i]:=j;
    BlueLUT[i]:=j;
  end;
 end;
UpdateLUT;
end;


macro '(-' begin end;



macro 'correct INCP close 90 gap [Z]';
var
i,j,l,minu,maxu,diffu,mino,maxo,diffo:integer;
n,mean,mode,min,max:integer;
begin
Measure;
GetResults(n,mean,mode,min,max);
ShowHistogram;
minu:= GetNumber('lower min (1): ',min); 
maxu:= GetNumber('lower max (90): ',90);
mino:=(90-maxu)+91;
mino:= GetNumber('upper min (91): ',mino);
maxo:=181-minu;
maxo:= GetNumber('upper max (180): ',maxo);
diffu:=maxu-minu+1;
diffo:=maxo-mino+1;
  for j:=1 to minu-1 do begin
    RedLUT[j]:=255;
    GreenLUT[j]:=255;
    BlueLUT[j]:=255;
  end;
  for j:=minu to maxu do begin
    i:=(j-minu)*89/diffu;
    RedLUT[j]:=255-i;
    GreenLUT[j]:=255-i;
    BlueLUT[j]:=255-i;
  end;
  for j:=maxu to 90 do begin
    RedLUT[j]:=255-90;
    GreenLUT[j]:=255-90;
    BlueLUT[j]:=256-90;
  end;
  for j:=91 to mino-1 do begin
    RedLUT[j]:=255-91;
    GreenLUT[j]:=255-91;
    BlueLUT[j]:=256-91;
  end;
  for j:=mino to maxo do begin
    i:=(j-mino)*89/diffo+91;
    RedLUT[j]:=255-i;
    GreenLUT[j]:=255-i;
    BlueLUT[j]:=255-i;
  end;
  for j:=maxo+1 to 180 do begin
    RedLUT[j]:=255-180;
    GreenLUT[j]:=255-180;
    BlueLUT[j]:=255-180;
  end;
  UpdateLUT;
ApplyLUT;
Measure;
ShowHistogram;

end;



macro '(-' begin end;

macro 'mirror AZI about NS     [M]';
var
i,j,rot,delta: integer;

begin

PutMessage('This macro mirrors the c-axes about the image vertical, i.e., it inverts the value of the azimuth image : azi_new = 180-azi_old ');

ShowHistogram;

 for i:=1 to 180 do begin
  j := 255 - (180-i);
    RedLUT[i]:= j;
    GreenLUT[i]:= j;
    BlueLUT[i]:= j;
    end;

  applyLUT;
  ShowHistogram;

end;


macro 'rotate AZI about z-axis [R]';
var
i,j,rot,delta: integer;

begin

PutMessage('This macro rotates the c-axes about the image normal, i.e., it shifts the value of the azimuth image by D degrees. Use rotations (-180 < rot < +180), clockwise is positive.');

ShowHistogram;
rot:= GetNumber('angle of rotation: ',30); 

if rot > 0 then begin
delta:=180-rot;

 for i:=1 to delta do begin
  j := 255 - (i+rot);
    RedLUT[i]:= j;
    GreenLUT[i]:= j;
    BlueLUT[i]:= j;
    end;
 for i:=delta+1 to 180 do begin
  j := 255 - (i-delta);
    RedLUT[i]:= j;
    GreenLUT[i]:= j;
    BlueLUT[i]:= j;
    end;

  applyLUT;
  ShowHistogram;

end;

if rot < 0 then begin

rot:=-rot;
delta:=180-rot;
 for i:=1 to rot do begin
  j := 255 - (i+delta);
    RedLUT[i]:= j;
    GreenLUT[i]:= j;
    BlueLUT[i]:= j;
    end;
 for i:=rot+1 to 180 do begin
  j := 255 - (i-rot);
    RedLUT[i]:= j;
    GreenLUT[i]:= j;
    BlueLUT[i]:= j;
    end;

  applyLUT;
  ShowHistogram;

end;

 end;


macro '(-' begin end;



macro 'make flat grains, part [1]';
var
  i,j,x,y,z,azi,inc,angN,angE,angH:integer;
 xx,yy,dummy,n,mean,mode,min,max:integer;
  intv,nx,ny,nxy,RoiLeft,RoiTop,RoiWidth,RoiHeight:integer;
dummya,dummyi,dummyg,dummy1,dummy2:integer;

begin

PutMessage('Use stack with 5 slices: 1-azi, 2-inc, 3-INVERSE grain boundary map (=areas),4-/5-empty grain boundary map -> results');

CheckForStack;

dummyg:= GetNumber('slice no of INVERSE grain boundary map ',3);
ChooseSlice(dummyg);
SetScale(0,'pixel');
ResetCounter;
SetCounter(0);
SetOptions('x-y center');
SetParticleSize(1,99999);
AnalyzeParticles('reset');
dummy:=rCount;
dummy:=GetNumber('n=',dummy);
ShowResults;
end;


macro 'make flat grains, part [2]';
var
  i,j,x,y,z,azi,inc,angN,angE,angH:integer;
number, xx,yy,dummy,n,mean,mode,min,max:integer;
  intv,nx,ny,nxy,RoiLeft,RoiTop,RoiWidth,RoiHeight:integer;
dummya,dummyi,dummyg,dummy1,dummy2:integer;

begin

PutMessage('Use stack with 5 slices: 1-azi, 2-inc, 3-areas,4-/5-empty grain boundary map -> results');

 CheckForStack;

dummya:= GetNumber('slice no of azi ',1);
dummyi:= GetNumber('slice no of inc ',2);
dummyg:= GetNumber('slice no of grain area map ',3);
dummy1:= GetNumber('slice no of azi results ',4);
dummy2:= GetNumber('slice no of inc results ',5);

number:=GetNumber('no.of particles?',rCount);

for i:= 1 to number do begin

xx:=rX[i];
yy:=rY[i];


ChooseSlice(dummyg);
SetThreshold(10);
MakeBinary;
 
 AutoOutline(xx,yy);

 ChooseSlice(dummya);
 Measure;
 GetResults(n,mean,mode,min,max);
 azi:=mode;

 ChooseSlice(dummyi);
 Measure;
 GetResults(n,mean,mode,min,max);
 inc:= mode;

ChooseSlice(dummy1);
SetForegroundColor(azi);
Fill;

ChooseSlice(dummy2);
SetForegroundColor(inc);
Fill;

end;
end;


